package website.digitalocean.chapter16;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: longhc@citycloud.com.cn
 * @since: 2023/2/16 9:51
 * @version: 1.0
 */
public class ShoppingCar {

    /**
     * 购物车列表
     */
    private List<Item> itemList;

    public ShoppingCar() {
        itemList = new ArrayList<>();
    }

    /**
     * 添加购物车
     *
     * @param item Item
     */
    public void add(Item item) {
        itemList.add(item);
    }

    /**
     * 移除购物车
     *
     * @param item Item
     */
    public void remove(Item item) {
        itemList.remove(item);
    }

    /**
     * 计算价格
     *
     * @return int
     */
    public int calculateTotal() {
        int sum = 0;
        for (Item item : itemList) {
            sum += item.getPrice();
        }
        return sum;
    }

    /**
     * 支付
     *
     * @param paymentStrategy PaymentStrategy
     */
    public void pay(PaymentStrategy paymentStrategy) {
        int total = calculateTotal();
        paymentStrategy.pay(total);
    }
}
